<?php


namespace apps\marriage\app\op\controller;


use apps\marriage\core\service\ServicePackageOrderService;
use apps\marriage\core\service\ServicePackageService;
use apps\marriage\core\service\UserService;
use think\Exception;
use think\Request;

class ServicePackageOrderController extends BaseMarriageOpController
{
    /**
     * @var ServicePackageOrderService
     */
    private $servicePackageOrderService;

    /**
     * @var ServicePackageService
     */
    private $servicePackageService;

    /**
     * @var UserService
     */
    private $userService;

    protected function init()
    {
        parent::init();
        $this->servicePackageOrderService = service('ServicePackageOrder', SERVICE_NAMESPACE);
        $this->servicePackageService = service('ServicePackage', SERVICE_NAMESPACE);
        $this->userService = service('User', SERVICE_NAMESPACE);
    }

    public function lists(Request $request)
    {
        $pageSize = $request->param('page_size', 10);
        $orderSn = $request->param('order_sn', '');
        $servicePackageId = $request->param('service_package_id', 0, 'intval');
        $params = [];
        if(!empty($orderSn)) {
            $params['order_sn'] = $orderSn;
        }
        if($servicePackageId > 0) {
            $params['service_package_id'] = $servicePackageId;
        }

        $res = $this->servicePackageOrderService->pageListByParams($params, $pageSize);
        if($res['data']) {
            $dataList = [];
            foreach ($res['data'] as $rs) {
                $servicePackage = $this->servicePackageService->getByPk($rs['service_package_id']);
                $user = $this->userService->getByPk($rs['user_id']);
                $dataList[] = [
                    'package' => $servicePackage,
                    'order' => $this->servicePackageOrderService->format($rs),
                    'user' => $user
                ];
            }
            $res['data'] = $dataList;
        }
        return $this->success($res);
    }

    public function detail(Request $request)
    {
        $orderSn = $request->param('order_sn', '');
        $order = $this->servicePackageOrderService->getByOrderSn($orderSn);
        if(empty($order)) {
            throw new Exception('订单不存在');
        }
        $servicePackage = $this->servicePackageService->getByPk($order['service_package_id']);
        $user = $this->userService->getByPk($order['user_id']);
        return $this->success([
            'package' => $servicePackage,
            'order' => $order,
            'user' => $user
        ]);
    }

    public function cancel(Request $request)
    {
        $orderSn = $request->param('order_sn', '');
        $this->servicePackageOrderService->onCancel($orderSn);
        return $this->success();
    }

    public function modify_money(Request $request)
    {
        $orderSn = $request->param('order_sn', '');
        $orderMoney = $request->param('order_money', 0.00, 'floatval');
        $this->servicePackageOrderService->onModifyOrderMoney($orderSn, $orderMoney);
        return $this->success();
    }
}